//
//  GameCollectionViewController.swift
//  MILHorizontalCollectionViewExample
//
//  Created by dengwei on 16/5/10.
//  Copyright © 2016年 IBM. All rights reserved.
//

import UIKit

class GameCollectionViewController: UICollectionViewController {
    
    let reuseIdentifier = "GameCell"
    var game: [Game]? {
        didSet {
            if game?.count == 0 {
                collectionView?.hidden = true
            } else {
                collectionView?.hidden = false
                collectionView?.reloadData()
            }
        }
    }
    //定义数组
    /*
    let courses = [
        ["name":"Swift","pic":"g1.png"],
        ["name":"OC","pic":"g2.png"],
        ["name":"java","pic":"g3.png"],
        ["name":"php","pic":"g4.png"],
        ["name":".net","pic":"g5.png"],
        ["name":"c++","pic":"g6.png"],
        ["name":"vb","pic":"g7.png"],
        ["name":"delphi","pic":"g8.png"],
        ["name":"asp","pic":"g1.png"],
        ["name":"phyton","pic":"g2.png"],
        ["name":"wangzhenting","pic":"g3.png"],
        ["name":"dengwei","pic":"g4.png"]
    ]*/
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        buildCollectionView()
    }
    
    func buildCollectionView(){
        
        self.collectionView!.showsHorizontalScrollIndicator = false;
        
        let collectionPageViewLayout : GameCollectionViewFlowLayout = GameCollectionViewFlowLayout()
        self.collectionView!.setCollectionViewLayout(collectionPageViewLayout, animated: false);
        self.collectionView!.showsHorizontalScrollIndicator = false;
        
        self.collectionView!.backgroundColor = UIColor.whiteColor()
        
        self.collectionView!.registerNib(UINib(nibName:"GameCell", bundle:nil), forCellWithReuseIdentifier: reuseIdentifier)
        
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
}


extension GameCollectionViewController{
    
    
   override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    
        return 1
    }
    
   override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
    //return courses.count
     return game?.count ?? 0
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let reuseIdentifier = "GameCell"
        // 获取设计的单元格，不需要再动态添加界面元素
        let cell = (self.collectionView?.dequeueReusableCellWithReuseIdentifier(
            reuseIdentifier, forIndexPath: indexPath))! as! GameCell
        // 从界面查找到控件元素并设置属性
        //方法1
        let games = game![indexPath.row]
        let titles = games.title
        let images = games.logo
       // print("titles=\(titles)")
       // print("images=\(images)")
        cell.titleLabel.text = titles!
        cell.imageView1.sd_setImageWithURL(NSURL(string: images!), placeholderImage: UIImage(named: "placeholder_gray.jpg"))
        //方法2
        //cell.imageView1.image = UIImage(named: courses[indexPath.item]["pic"]!)
        //cell.titleLabel.text =  courses[indexPath.item]["name"]!
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        //方法1
        let games = game![indexPath.row]
        let titles = games.title
        let images = games.logo
        //方法2
       // let titles = game![indexPath.item]["title"]!
        print("titles=\(titles!)")
        print("images=\(images!)")
        print("row=\(indexPath.row)")
        
    }
}